package com.ovopark.firecar.recyclerview.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;


import com.ovopark.firecar.recyclerview.callback.OnClickListenerCallBack;
import com.ovopark.firecar.recyclerview.callback.SingleItem;
import com.ovopark.firecar.recyclerview.delegate.ItemViewDelegate;
import com.ovopark.firecar.recyclerview.delegate.ItemViewDelegateManager;
import com.ovopark.firecar.recyclerview.viewholder.BaseRecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Created by DaXian on 2018/6/18.

 */

public class KingRecyclerViewAdapter<T> extends RecyclerView.Adapter<BaseRecyclerViewHolder> implements OnClickListenerCallBack {
    private static final String TAG = "KingRecyclerViewAdapter";
    protected List<T> mData = new ArrayList<>();
    protected LayoutInflater mInflater;
    protected ItemViewDelegateManager mManager;
    protected Context mContext;
    protected int mPosition;

    public KingRecyclerViewAdapter() {

    }

    public List<T> getData() {
        return mData;
    }

    private RecyclerView mRecyclerView;

    /**
     * 单布局
     *
     * @param context
     * @param layoutid
     * @param singleItem
     */
    public KingRecyclerViewAdapter(Context context, int layoutid, SingleItem<T> singleItem) {
        this(context, null, layoutid, singleItem);
    }

    /**
     * 继承KingRecyclerViewAdapter  需要实现该方法
     *
     * @param context
     */
    protected KingRecyclerViewAdapter(Context context) {
        this(context, null, new ItemViewDelegate[]{null});
    }

    /**
     * 继承KingRecyclerViewAdapter  需要实现该方法
     *
     * @param context
     */
    protected KingRecyclerViewAdapter(Context context, List<T> data) {
        this(context, data, new ItemViewDelegate[]{null});
    }

    /**
     * 多布局
     *
     * @param context
     * @param delegate
     */
    public KingRecyclerViewAdapter(Context context, @Nullable ItemViewDelegate<T>... delegate) {
        this(context, null, delegate);
    }

    public KingRecyclerViewAdapter(Context context, List<T> data, final int layoutId, final SingleItem<T> singleItem) {
        this(context, data, new ItemViewDelegate<T>() {
            @Override
            public int getItemViewLayoutId() {
                return layoutId;
            }

            @Override
            public boolean isForViewType(T item, int position) {
                return true;
            }

            @Override
            public void convert(BaseRecyclerViewHolder holder, T t, int position) {
                singleItem.bindData(holder, t, position);
            }
        });
    }

    public KingRecyclerViewAdapter(Context context, List<T> data, ItemViewDelegate<T>... delegate) {
        if (data != null) {
            this.mData = data;
        } else {
            mData = new ArrayList<>();
        }
        if (context != null) {
            this.mContext = context;
            mInflater = LayoutInflater.from(mContext);
        } else {
            Log.e(TAG, "KingAdapter: mContext is null");
            return;
        }
        mManager = new ItemViewDelegateManager();
        if (delegate != null) {
            for (ItemViewDelegate<T> tItemViewDelegate : delegate) {
                addItemViewDelegate(tItemViewDelegate);
            }
        }
    }


    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewDelegate itemViewDelegate = mManager.getItemViewDelegate(viewType);
        int layoutId = itemViewDelegate.getItemViewLayoutId();
        View view = mInflater.inflate(layoutId, parent, false);
        setLayoutParams(view);
        BaseRecyclerViewHolder holder = new BaseRecyclerViewHolder(view, mPosition, layoutId, mContext, this);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder holder, @SuppressLint("RecyclerView") int position) {
        this.mPosition = position;
        convert(holder, mData.get(position));
    }

    public void convert(BaseRecyclerViewHolder holder, T t) {
        mManager.convert(holder, t, holder.getAdapterPosition());
    }

    public void setLayoutParams(View view) {

    }


    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        Log.e(TAG, "getItemCount: data  size  is zero");
        return 0;
    }

    /**
     * 添加布局
     *
     * @param itemViewDelegate
     * @return
     */
    public KingRecyclerViewAdapter addItemViewDelegate(ItemViewDelegate<T> itemViewDelegate) {
        mManager.addDelegate(itemViewDelegate);
        return this;
    }

    @Override
    public void onClickListenerCallBack(int position, View view, BaseRecyclerViewHolder holder) {
        onItemClick(mData.get(holder.getAdapterPosition()), view, holder);
    }

    /**
     * 如果需要添加点击事件  重写这个方法
     *
     * @param bean
     * @param view
     * @param holder
     */
    protected void onItemClick(T bean, View view, BaseRecyclerViewHolder holder) {

    }

    @Override
    public int getItemViewType(int position) {
        if (!useItemViewDelegateManager()) {
            return super.getItemViewType(position);
        }
        return mManager.getItemViewType(mData.get(position), position);
    }

    private boolean useItemViewDelegateManager() {
        return mManager != null && mManager.getItemViewDelegateCount() > 0;
    }

    public void updata(List<T> data) {
        if (data != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        } else {
            mData.clear();
            notifyDataSetChanged();
        }
    }

    public void addTop(T bean) {
        if (bean != null) {
            mData.add(0, bean);
            notifyDataSetChanged();
        }
    }

    public void add(T bean) {
        if (bean != null) {
            mData.add(bean);
            notifyDataSetChanged();
        }
    }

    public void add(int position, T bean) {
        if (bean != null) {
            mData.add(position, bean);
            notifyDataSetChanged();
        }
    }


    public void addAll(List<T> data) {
        if (data != null) {
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }


    public RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    public void setRecyclerView(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
    }
}
